package com.situ.day3;

import org.junit.Test;

public class ArrayDemo {
    @Test
    public void test1() {
        int num = 3;
        int score1 = 84;
        int score2 = 54;
        int score3 = 64;
        //数组：存放同一种类型数据的集合，在内存里面是开辟一块连续的区域。
        int[] array = new int[4];
        System.out.println("length: " + array.length);//4
        array[0] = 33;
        array[1] = 2;
        array[2] = 45;
        array[3] = 19;
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("-------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }

    }

    @Test
    public void test44() {
        int[] array1 = new int[3];
        boolean[] array2 = new boolean[3];
        double[] array3 = new double[3];
        //Student[] array4 = new Student[3];

        //1、在定义的时候直接赋值
        int[] array = {2, 3, 66};
        int[] arr = new int[]{34, 6, 78, 79};
    }

    @Test
    public void test45() {
        int[] array = new int[4];
        array[0] = 33;
        array[1] = 2;
        array[2] = 45;
        array[3] = 19;
        for (int i = 0; i <= array.length; i++) {
            //java.lang.ArrayIndexOutOfBoundsException: 4
            System.out.println(array[i]);
        }
    }

    //数组最重要操作就是遍历。
    //只要能遍历所有元素：求最大值、最小值、排序。
    @Test
    public void test23(){
        int[] array = {23, 45, 67, 2, 12};
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test333() {
        int[] array = {23, 45, 67, 2, 12};
        int maxValue = getMax(array);
        System.out.println(maxValue);
    }

    //参数，形参
    /**
     * 返回数组的最大值
     * @param array 传递过来的数组
     * @return 数组最大值
     */
    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test24(){
        int[] array = {23, 45, 67, 2, 12};
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }


    @Test
    public void test22() {
        //i=1   j=
        int[] array = {40, 17, 21, 1}; // 1, 17,21,40
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    private void sort(int[] array) {
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    @Test
    public void test222() {
        int[][] array = new int[3][];
        array[0] = new int[2];
        array[1] = new int[2];
        array[2] = new int[3];
        array[0][0] = 23;
        array[0][1] = 12;
        array[1][0] = 22;
        array[1][1] = 22;
        array[2][0] = 21;
        array[2][1] = 22;
        array[2][2] = 23;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }
}
